package com.pagoda.nerp.web.services;


import com.pagoda.nerp.web.common.buildTree.Tree;
import com.pagoda.nerp.web.common.buildTree.TreeBuild;
import com.pagoda.nerp.web.entities.HRORGPost;
import com.pagoda.nerp.web.entities.HRUnit;
import com.pagoda.nerp.web.entities.HRZGZD;
import com.pagoda.nerp.web.repositories.HRUnitDao;
import com.pagoda.nerp.webapi.hander.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HRUnitService {
    @Autowired
    private HRUnitDao hrUnitDao;
    @Autowired
    private EHRService ehrService;

    /**
     * @Description: 获取查询结果数据
     * @Pram: 根节点的orguid
     * @Date: 10:32 2017/12/27
     */
    public ResponseResult getHRUnitTree(String orguid) {
        //获取到根节点信息
        HRUnit hrUnit = hrUnitDao.findOne(orguid);
        //获取根节点树编码
        String rootCanonicalcode = hrUnit.getCanonicalcode();
        //获取以它为根的子树数据，数据有序
        List<HRUnit> list = hrUnitDao.findData(rootCanonicalcode);

        List<Tree> answer = TreeBuild.buildTree(list);

        return ResponseResult.success(answer);
    }


    /**
     * @Description: 保存数据
     * @Date: 15:14 2018/1/3
     */
    public void save(List<HRUnit> list) {
        hrUnitDao.save(list);
    }

    public Date getModifyTime() {
        HRUnit hrUnit = hrUnitDao.findOne("updatetime");
        //return hrUnit.getModifytime();
        return hrUnit == null ? new Date(1525276800000L) : hrUnit.getModifytime();

    }

    public List<HRUnit> getUpdateList(Date modifydate) {
        List<HRUnit> list = new ArrayList<>();

        Calendar current = Calendar.getInstance();  //获取当前的日历时间
        Calendar modify = Calendar.getInstance();
        modify.setTime(modifydate);

        while (modify.before(current)) {
            DateFormat df = new SimpleDateFormat("yyyyMMdd");   //时间格式
            String date = df.format(modify.getTime());
            //System.out.println("HRUnitService.getUpdateList " + date);
            List<HRUnit> add = ehrService.getHRORGINFOList(date);   //获取HRUnit数据
            if (add != null) list.addAll(add);
            modify.add(Calendar.DAY_OF_YEAR, 1); //当前时间加一天
        }
        return list;
    }

    /**
     * @Description:修改最后更新时间
     * @Date: 17:57 2018/1/8
     */
    public void modifyUpdateTime() {
        Calendar calendar = Calendar.getInstance();
        HRUnit hrUnit = hrUnitDao.findOne("updatetime");
        hrUnit.setModifytime(calendar.getTime());
        hrUnitDao.save(hrUnit);
    }

    /**
     * @Description:更新unit表
     * @Date: 17:58 2018/1/8
     */
    public List<HRUnit> updateDBUnit() {
        //获取更新list
        Date modifydate = getModifyTime();  // 获得updatetime字段
        List<HRUnit> updateList = getUpdateList(modifydate);    // 获得更新字段集合(去HR数据库拿数据)
        for (HRUnit hrUnit : updateList) {

            String parentnm = hrUnit.getParentnm(); //上级机构ID
            HRUnit parent = hrUnitDao.findOne(hrUnit.getParentnm());    // 上级机构
//            System.out.println(parentnm);
            if (parent != null) {
                String parentcode = parent.getCanonicalcode();  //获取机构树编码
                hrUnit.setCanonicalparentcode(parentcode);  //设置机构树父亲编码
                hrUnit.setCanonicalcode(parentcode + "/" + hrUnit.getOrguid()); //设置机构树编码(字段太长了)
            } else
                hrUnit.setCanonicalcode(hrUnit.getOrguid());
            hrUnitDao.save(hrUnit); // 存入数据库
        }
        modifyUpdateTime(); //修改最后更新时间
        System.out.println("UpdateDBUnit Complete!");
        return updateList;
    }

    //modify HRUnit Table ,add canonicalcode and canonicalparentcode data

    /**
     * @Description: 更新hrunit表中canonicalcode、canonicalparentcode数据
     * @Pram: rootnum根节点的orguid、currentParentnum目前节点的父亲编号,默认 集团 为根，从上往下遍历更新
     * @Date: 10:36 2017/12/27
     */
    public void addCanonicalCode(String rootnum, String currentParentnum) {
        //找到孩子节点
        List<HRUnit> list = hrUnitDao.findByParentnm(rootnum);

        //更新孩子节点的数据，深度遍历更新canonicalcode,canonicalparentcode
        for (HRUnit hrUnit : list) {
            String orguid = hrUnit.getOrguid();
            hrUnit.setCanonicalcode(currentParentnum + "/" + orguid);
            hrUnit.setCanonicalparentcode(currentParentnum);
            hrUnitDao.save(hrUnit);

            //深度遍历
            addCanonicalCode(orguid, currentParentnum + "/" + orguid);
        }
    }
}
